package com.wwg.market.ui.category.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.mobileares.midp.mui.client.form.UFishFlowForm;
import com.mobileares.midp.widgets.client.button.CButton;
import com.mobileares.midp.widgets.client.pop.PopPanel;
import com.mobileares.midp.widgets.client.textbox2.ILabelValidator;
import com.mobileares.midp.widgets.client.textbox2.LabelValidatorFactory;
import com.mobileares.midp.widgets.client.textbox2.TextAreaLabel;
import com.mobileares.midp.widgets.client.textbox2.TextBoxLabel;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.wwfish.gwt.model.client.IModelCallback;
import com.wwfish.gwt.model.client.ISaveAndUpdateModelProvider;
import com.wwfish.gwt.model.client.impl.FishModelManager;
import com.wwfish.gwt.model.client.impl.FormModel;
import com.wwfish.gwt.model.client.impl.ListModel;
import com.wwg.market.ui.dashboard.client.Utils;
import com.wwg.market.ui.service.dto.CategoryDto;
import com.wwg.market.ui.service.interfaces.CategoryService;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-11-22
 * Time: 14:24:23
 * To change this template use File | Settings | File Templates.
 */
public class CategoryForm {
    private UFishFlowForm<CategoryDto> form;
    private PopPanel pop;
    private ListModel<CategoryDto> listModel;
    private CategoryDto parent;

    public CategoryForm() {
        pop = new PopPanel(true);
        init();
        initFormModel();
    }

    public void setModel(ListModel<CategoryDto> model) {
        this.listModel = model;
    }

    private void init() {
        form = new UFishFlowForm<CategoryDto>(1);
        TextBoxLabel text = new TextBoxLabel("类别名称:");
        TextBoxLabel code = new TextBoxLabel("类别标示:");
        TextAreaLabel description = new TextAreaLabel("类别描述:");

        form.addElement(text, "name", new ILabelValidator[]{LabelValidatorFactory.require()});
        form.addElement(code, "code", new ILabelValidator[]{LabelValidatorFactory.require()});
        form.addElement(description, "description");
        pop.addBodyWidget(form);

        CButton confirm = new CButton("确定");
        confirm.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                form.getFishModel().saveAndUpdate(form.getFishWidget());
            }
        });
        CButton cancel = new CButton("取消");
        cancel.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                pop.hidden();
            }
        });
        pop.addFootButton(confirm);
        pop.addFootButton(cancel);

    }

    public void show(CategoryDto parent, CategoryDto dto) {
        this.parent = parent;
        form.getFishModel().load(dto);
        pop.show();
    }

    public void hidden() {
        pop.hidden();
    }

    public void setTitle(String text) {
        pop.setHeadText(text);
    }

    private void initFormModel() {
        FormModel model = (FormModel) FishModelManager.createTempModel(FishModelManager.TYPE_FORM);
        model.setSaveAndUpdateProvider(new ISaveAndUpdateModelProvider<CategoryDto>() {
            public void saveAndUpdate(final CategoryDto value, final IModelCallback callback) {
                if (parent != null) value.setParentId(parent.getId());
                Utils.invoke(CategoryService.class.getName(), "saveAndUpdate", new Object[]{value}, new LoadingAsyncCallback() {
                    @Override
                    protected void success(Object o) {
                        hidden();
                        callback.setFishModel(o);
                        if (value.getId() == null)
                            listModel.addItem(0, (CategoryDto) o);
                        else
                            listModel.updateItem(value, (CategoryDto) o);
                        if (parent != null)
                            parent.setChildren(listModel.getModel());

                    }
                });
            }
        });
        form.setFishModel(model);

    }
}
